package com.youhome.api.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.api.dto.HouseItemResult;
import com.youhome.api.dto.PageResult;
import com.youhome.api.dto.Result;
import com.youhome.api.dto.ResultCode;
import com.youhome.api.dto.swagger.*;
import com.youhome.common.exception.CustomException;
import com.youhome.service.model.ListHubList;
import com.youhome.service.service.ListHubPhotoService;
import com.youhome.service.service.ListHubService;
import com.youhome.service.service.SolrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/05/21.
 */
@RestController
@Api(tags = "房源")
@RequestMapping("/homes")
public class HomeController {

    @Autowired
    private ListHubService listHubService;
    @Autowired
    private ListHubPhotoService listHubPhotoService;
    @Autowired
    private SolrService solrService;

    @ApiOperation(value = "房源列表", notes = "房源列表", response = SHouseResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "zipcode", value = "邮编", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "currentPage", value = "当前页码(默认第一页)", paramType = "query")
    })
    @GetMapping(value = "/selling/{zipcode}")
    public Object list(@PathVariable String zipcode, Integer currentPage) throws CustomException {

        Integer pageSize = 20;
        PageInfo<ListHubList> listHubListPageInfo = solrService.searchByZipOfApi(zipcode, currentPage, pageSize);
        ModelMapper modelMapper = new ModelMapper();
        PageInfo<HouseItemResult> pageInfo = modelMapper.map(listHubListPageInfo, new TypeToken<PageInfo<HouseItemResult>>() {
        }.getType());
        return new PageResult(ResultCode.SUCCESS, pageInfo);
    }

    @ApiOperation(value = "房源图片", notes = "房源图片", response = SPhotoResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/photos")
    public Object photo(@PathVariable String houseId) {
        return new Result(ResultCode.SUCCESS, listHubPhotoService.selectTop10ByBean(houseId));
    }

    @ApiOperation(value = "房源详情", notes = "房源详情", response = SHouseDetailsResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/detail")
    public Object detail(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectDetailsByHouseIdOfApi(houseId));
    }

    @ApiOperation(value = "统计", notes = "统计", response = SStatsResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/stats")
    public Object stats(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectStatsOfApi(houseId));
    }

    @ApiOperation(value = "供应商", notes = "供应商", response = SProviderResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/provider")
    public Object provider(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectProviderByHouseId(houseId));
    }


}
